package com.adcc.loadsheet.monitor.util;

import com.adcc.aoc.devLib.common.log.Log;
import com.google.common.base.Strings;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;

/**
 * 用户信息
 */
@Component
public class UserFactory {

    // 用户信息配置路径
    private static final String USERS_CONFIG_PATH = "users.properties";

    // Properties
    private static Properties properties;

    @PostConstruct
    private void initialize() {
        FileInputStream fis = null;
        try{
            String path = FileUtil.getPath(USERS_CONFIG_PATH);
            File file = new File(path);
            fis = new FileInputStream(file);
            properties = new Properties();
            properties.load(fis);
        }catch (Exception ex){
            Log.error(UserFactory.class, "initialize() error", ex);
        }finally {
            if(fis != null){
                try {
                    fis.close();
                } catch (IOException ex) {
                    Log.error(UserFactory.class,"close io error",ex);
                }
            }
        }
    }

    public String getSaltValue(){
        return Strings.nullToEmpty(properties.getProperty("saltValue"));
    }

    public String getAdminUserName(){
        return Strings.nullToEmpty(properties.getProperty("adminUsername"));
    }

    public String getAdminPassword(){
        return Strings.nullToEmpty(properties.getProperty("adminPassword"));
    }


    public String getOperatorUserName(){
        return Strings.nullToEmpty(properties.getProperty("operatorUsername"));
    }

    public String getOperatorPassword(){
        return Strings.nullToEmpty(properties.getProperty("operatorPassword"));
    }


    public Properties getProperties(){
        return properties;
    }

    public String getProperty(String key){
        return properties.get(key).toString();
    }

    public void setProperty(String key,String value){
        properties.setProperty(key,value);
    }

    public void save(){
        FileWriter fw = null;
        try{
            String path = FileUtil.getPath(USERS_CONFIG_PATH);
            File file = new File(path);
            fw = new FileWriter(file);
            fw.write("#Users configuration settings\n");
            fw.write("saltValue=" + getSaltValue() + "\n");
            fw.write("adminUsername=" + getAdminUserName() + "\n");
            fw.write("adminPassword=" + getAdminPassword() + "\n");
            fw.write("operatorUsername=" + getOperatorUserName() + "\n");
            fw.write("operatorPassword=" + getOperatorPassword() + "\n");
            fw.flush();
        }catch (Exception ex){
            Log.error(UserFactory.class,"save() error",ex);
        }finally {
            if(fw != null){
                try {
                    fw.close();
                } catch (IOException ex) {
                    Log.error(UserFactory.class,"close io error",ex);
                }
            }
        }
    }

}
